﻿#include "financeservice.h"
#include "company.h"
#include <QMessageBox>
#include "user.h"

FinanceService::FinanceService(QObject *parent) : QObject(parent)
{

}

void FinanceService::addFinanceItem(QWidget *widget, QPushButton *button, int type, QString category, QString subject){
    if(subject.isEmpty()){
        QMessageBox::warning(widget, "警告", "输入内容不能为空！");
        return;
    }

    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::information(widget, "信息", message);
        emit this->addFinanceItemSuccess();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::warning(widget, "警告", message);
    });

    button->setEnabled(false);
    manager->addFinanceItem(Company::getCompanyId(), type, category, subject);
}

void FinanceService::getFinanceItems(QWidget *widget){
    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        Q_UNUSED(message);
        QJsonArray financeItemArr = data.value("financeItems").toArray();
        QList<FinanceItem*> financeItems;
        for(QJsonValue obj:financeItemArr){
            FinanceItem *financeItem = new FinanceItem();
            financeItem->loadFromJsonObject(obj.toObject());
            financeItems.append(financeItem);
        }
        this->getFinanceItemsSuccess(financeItems);
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->getFinanceItems(Company::getCompanyId());
}

void FinanceService::addFinanceRecord(QWidget *widget, QPushButton *button, FinanceItem *financeItem, double money, QString remark){
    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::information(widget, "信息", message);
        emit this->addFinanceRecordSuccess();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        button->setEnabled(true);
        QMessageBox::warning(widget, "警告", message);
    });

    button->setEnabled(false);
    manager->addFinanceRecord(User::getUsername(), financeItem->companyId, financeItem->itemId, money, remark);
}

void FinanceService::deleteFinanceItem(QWidget *widget, FinanceItem *financeItem){
    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        QMessageBox::information(widget, "信息", message);
        widget->close();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->deleteFinanceItem(financeItem->companyId, financeItem->itemId);
}

void FinanceService::updateFinanceItem(QWidget *widget, FinanceItem *financeItem){
    if(financeItem->subject.isEmpty()){
        QMessageBox::warning(widget, "警告", "输入科目不能为空！");
        return;
    }

    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        QMessageBox::information(widget, "信息", message);
        widget->close();
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->updateFinanceItem(
                financeItem->companyId,
                financeItem->itemId,
                financeItem->type,
                financeItem->category,
                financeItem->subject);
}

void FinanceService::financeQuery(
        QWidget *widget,
        int type1,
        int type2,
        QString startDate,
        QString endDate){
    manager = new FinanceNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        Q_UNUSED(message);
        QList<Message*> messages;
        for(QJsonValue objValue:data.value("messages").toArray()){
            Message *messageItem = new Message();
            messageItem->loadFromJsonObject(objValue.toObject());
            messages.append(messageItem);
        }

        emit this->financeQuerySuccess(messages);
    });

    manager->financeQuery(User::getUsername(), type1, type2, startDate, endDate);
}
